package com.apobates.jforum.grief.aurl.utils.defend;

import java.util.List;
import java.util.function.Supplier;
/**
 * 单值Assertion
 * 注意: 一个目标只有一条消息, 遇到第一个失败规则即中止校验
 * @param <T> 目标类型
 */
public class SingleAssertion<T> implements Assertion<T>{
    protected final Supplier<T> value;

    protected SingleAssertion(Supplier<T> value) {
        this.value = value;
    }

    public T getValue(){
        return value.get();
    }

    @Override
    public List<AssertResult<T>> asserte(PredicateQueue<T> predicateQueue) {
        boolean success = true;
        String message = "OK";
        String errorLabel = "basic";
        for (AssertPredicate<T> condition: predicateQueue.getConditions()) {
            if(!condition.predicate(getValue())){
                message = condition.getError();
                success = false;
                errorLabel = condition.getLabel();
                break;
            }
        }
        return List.of(new AssertResult(getValue(), success, message, 0, errorLabel));
    }
    /**
     * 转为全校验实例
     * @return
     */
    public SingleCompleteAssertion<T> toCompleteInstance(){
        return new SingleCompleteAssertion<>(this.value);
    }
}
